<?php

/**
 * CelebrioNews default
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    CelebrioNews
 */
namespace ApplicationsModule\NewsModule;
use WebtopModule\BasePresenter;

class DefaultPresenter extends BasePresenter {

    public function startup() {
	parent::startup();
	header ( "Content-type:text/html; charset=utf-8" ); // TODO: poresit
	if ($this->isAjax()) {
	    $this->invalidateControl("celebrionewsappSnippet");
	}
    }
    
    public function actionRateArticle() {
        if(isset($_POST["feed"])) {
            if(isset($_POST["guid"])) {
                $guid = $_POST["guid"];
            }
            $feed = htmlspecialchars($_POST["feed"]);
            $lang = $this->getUser()->getIdentity()->language;
            $newsReader = new NewsReader($lang);
            $newsReader->rateArticle($feed, $guid);
        }
    }

    public function renderDefault() {
        //$newsReader = new NewsReader($this->getUser()->getIdentity()->language);
        //$topArticles = $newsReader->getRatedArticles();
        //$this->template->news = $topArticles;
    }

    public function renderIframe(){
	$this->template->iframeUrl = $this->link("Default:story", array('type'=>\htmlspecialchars($_GET["type"])));
    }

    public function renderStory() {
        $userLanguage = $this->getUser()->getIdentity()->data["language"];
	$newsReader = new NewsReader($userLanguage);
        try {
            switch ($_GET["type"]) {
                case "topstories":
                    $this->template->news = $newsReader->getTopStories();
                    break;
                case "world":
                    $this->template->news = $newsReader->getWorld();
                    break;
                case "entertainmet":
                    $this->template->news = $newsReader->getEntertainment();
                    break;
                case "sports":
                    $this->template->news = $newsReader->getSports();
                    break;
                default:
                    $this->template->news = $newsReader->getTopStories();
            }
        } catch (\InvalidStateException $e) {
            $this->template->error = $e->getMessage();
        }
    }
    
    public function renderRatedArticles() {
        $userLanguage = $this->getUser()->getIdentity()->data["language"];
	$newsReader = new NewsReader($userLanguage);
        if(($_GET["type"]) == "rated") {
            $this->template->news = $newsReader->getRatedArticles();
        }
    }

}